<template>
  <div class="app-container">
    <div v-if="type != 1">
      <div>
        <el-button type="primary" @click="goAdd">新建赛事 </el-button>
      </div>
      <br />
      <!-- 搜索区域 -->
      <el-form ref="queryRef" :model="queryParams" :inline="true">
        <el-form-item label="赛事名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="赛事名称"
            clearable
            style="width: 240px"
            @keyup.enter="searchFN"
          />
        </el-form-item>

        <el-form-item label="发布状态" prop="type">
          <el-select v-model="queryParams.releaseStatus">
            <el-option label="已发布" value="1" />
            <el-option label="未发布" value="0" />
            <el-option label="全部" value="-1" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="searchFN"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <el-table :data="list" @sort-change="sortChange">
      <el-table-column label="序号" align="center" type="index" width="50" />
      <el-table-column
        label="赛事类型"
        align="center"
        prop="typeStr"
        min-width="80"
      />
      <el-table-column
        label="项目"
        align="center"
        prop="projectName"
        min-width="100"
      >
      </el-table-column>

      <el-table-column
        label="赛事logo"
        align="center"
        prop="memberName"
        min-width="80"
      >
        <template #default="props">
          <el-image
            style="width: 50px; height: 50px"
            :src="props.row.logo"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="赛事名称"
        align="center"
        prop="cptName"
        min-width="160"
      />
      <el-table-column
        label="赛事时间"
        align="center"
        prop="matchTimeStr"
        min-width="240"
        :sortable="'custom'"
      />
      <!-- <el-table-column
        label="报名时间"
        align="center"
        prop="enrollTimeStr"
        min-width="300"
        :sortable="'custom'"
      /> -->
      <el-table-column
        label="赛事项目"
        align="center"
        prop=""
        min-width="80"
        v-if="type != 1"
      >
        <template #default="props">
          <el-button
            type="text"
            v-if="props.row.type == 0"
            @click="gocompetitionPro(props.row)"
            >去完善</el-button
          >
        </template>
      </el-table-column>

      <el-table-column
        v-if="type != 1"
        label="项目费用"
        align="center"
        prop="costCounts"
        min-width="80"
      >
        <template #default="props">
          <el-button
            type="text"
            v-if="props.row.type == 0"
            @click="costFN(props.row)"
            >去完善</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="type != 1"
        label="日程"
        align="center"
        prop="balance"
        min-width="80"
      >
        <template #default="props">
          <el-button
            type="text"
            v-if="props.row.type == 0"
            @click="goSchedule(props.row)"
            >去完善</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="设置"
        align="center"
        prop="balance"
        min-width="80"
      >
        <template #default="props">
          <el-button
            type="text"
            @click="settings(props.row)"
            >设置</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="审核状态"
        align="center"
        prop="status"
        min-width="120"
      >
        <template #default="props">
          <el-button type="text" v-if="props.row.status == 0">待审核</el-button>
          <el-button type="text" v-else>审核通过</el-button>
        </template>
      </el-table-column> -->
      <el-table-column
        label="报名状态"
        align="center"
        prop="enrollStatusStr"
        min-width="80"
      />
      <el-table-column
        label="发布状态"
        align="center"
        prop="statusStr"
        min-width="80"
      />

      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding"
        width="240"
      >
        <template #default="props">
          <el-button
            type="text"
            v-if="props.row.statusStr == '未发布'"
            @click="viewRow(props.row)"
            >编辑</el-button
          >
          <el-button
            v-if="type != 1"
            type="text"
            @click="updateRow(props.row)"
            >{{
              props.row.statusStr == "未发布" ? "发布" : "取消发布"
            }}</el-button
          >

          <el-button
            v-if="type != 1"
            type="text"
            @click="editDisCount(props.row)"
            >{{ props.row.topFlag == 1 ? "取消置顶" : "置顶" }}</el-button
          >

          <el-button type="text" @click="consumeList(props.row)"
            >数据</el-button
          >
          <el-button
            type="text"
            v-if="props.row.statusStr == '未发布'"
            @click="delList(props.row)"
            >删除</el-button
          >
          <!-- <el-button
            type="text"
            @click="settings(props.row)"
            >设置</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="initData"
    />
  </div>
</template>

<script setup>
import { useRouter, useRoute } from "vue-router";
import { getCurrentInstance } from "vue";
import { reactive } from "@vue/runtime-core";
import {
  overhead,
  cancel,
  publish,
  disqualification,
  delApi,
} from "@/api/match/list.js";
import { leagueParticulars } from "@/api/match/league.js";

const { proxy } = getCurrentInstance();
const total = ref(0);
const list = ref([]);
const router = useRouter();
const route = useRoute();
const id = ref(route.query.id);
const type = ref(route.query.type);
const data = reactive({
  queryParams: {
    type: "-1",
    pageNum: 1,
    pageSize: 10,
    status: -1,
    releaseStatus: "-1",
    name: "",
    sourceFlag: 0,
    matchOrder: 0,
    signOrder: 0,
  },
  form: {},
});
const { queryParams, form } = toRefs(data);

onMounted(() => {
  initData();
});
// 获取赛会列表
async function initData() {
  queryParams.value.leagueId = id.value;
  let res = await leagueParticulars(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
}

// 搜索
function searchFN() {
  initData();
}

// 重置
function resetQuery() {
  queryParams.value = {};
  queryParams.value.sourceFlag = 0;
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  queryParams.value.matchOrder = 0;
  queryParams.value.signOrder = 0;
  initData();
}

// 置顶/取消置顶
async function editDisCount(row) {
  if (row.topFlag == 0) {
    let res = await overhead(row.id);
    if (res.code == 200) {
      initData();
      proxy.$modal.msgSuccess("操作成功");
    } else {
      proxy.$modal.msgError("操作失败");
    }
  } else {
    let res = await cancel(row.id);
    if (res.code == 200) {
      initData();
      proxy.$modal.msgSuccess("操作成功");
    } else {
      proxy.$modal.msgError("操作失败");
    }
  }
}

// 发布
async function updateRow(row) {
  form.value.id = row.id;
  if (row.statusStr == "未发布") {
    let res = await publish(form.value);
    if (res.code == 200) {
      initData();
      proxy.$modal.msgSuccess("操作成功");
    } else {
      proxy.$modal.msgError("操作失败");
    }
  } else {
    // 取消发布
    form.value.id = row.id;
    let res = await disqualification(form.value);
    if (res.code == 200) {
      initData();
      proxy.$modal.msgSuccess("操作成功");
    } else {
      proxy.$modal.msgError("操作失败");
    }
  }
}

// 降序升序
function sortChange(column) {
  // 升序
  if (column.prop == "enrollTimeStr") {
    // console.log(column.prop);
    // 报名
    if (column.order == "ascending") {
      queryParams.value.signOrder = 1;
    } else if (column.order == "descending") {
      queryParams.value.signOrder = 0;
    }
  }

  if (column.prop == "matchTimeStr") {
    // console.log(column.prop);
    // 赛事
    if (column.order == "ascending") {
      queryParams.value.matchOrder = 1;
    } else if (column.order == "descending") {
      queryParams.value.matchOrder = 0;
    }
  }

  initData();
}

// 日程
function goSchedule(row) {
  router.push({
    path: "/match/schedule",
    query: { id: row.id },
  });
}

// 赛事项目
function gocompetitionPro(row) {
  router.push({
    path: "/match/competitionPro",
    query: {
      id: row.id,
      baseProjectId: row.baseProjectId,
    },
  });
}

// 费用
function costFN(row) {
  router.push({
    path: "/match/cost",
    query: { id: row.id },
  });
}

// 数据
function consumeList(row) {
  router.push({
    path: "/match/competitiondata",
    query: { id: row.id },
  });
}

// 设置
function settings(row) {
  router.push({
    path: "/match/setting",
    query: { id: row.id },
  });
}


// 编辑
function viewRow(row) {
  router.push({
    path: "/match/addList",
    query: { id: row.id },
  });
}

// 删除
function delList(row) {
  proxy.$modal
    .confirm('是否确认删除赛事名称为"' + row.cptName + '"的数据项？')
    .then(async function () {
      form.value.ids = row.id;
      await delApi(form.value);
    })
    .then(async () => {
      await initData();
      proxy.$modal.msgSuccess("操作成功");
    })
    .catch(() => {});
}

// 联赛
function goAdd() {
  router.push({
    path: "/match/addList",
    query: {
      leagueId: id.value,
    },
  });
}
</script>

<style lang="scss" scoped>
.router {
  margin-right: 20px;
}
</style>
